package services.socialuser.util;

import play.Logger;
import models.User;
import securesocial.provider.AuthenticationMethod;
import securesocial.provider.ProviderType;
import securesocial.provider.SocialUser;
import securesocial.provider.UserId;

public class ModelUserAdaptor implements UserAdapter
{
	private User user;
	
	public ModelUserAdaptor(User user)
	{
		this.user = user;
	}
	
	public SocialUser getUser() 
	{
		if (user == null) 
			return null;
		
		SocialUser su = new SocialUser();
		
		su.id = new UserId();
		su.id.provider = ProviderType.valueOf(user.provider);
		
		try
		{
			
			String provider = user.stringID.split( DELIMITER )[0];
			String string = ProviderType.valueOf( provider ).toString();
			
			if( su.id.provider.toString().equals( string ) )
			{
				su.id.id = user.stringID.substring( user.stringID.indexOf( CHAR_DELIMITER ) + 1 );
			}
			else
			{
				su.id.id = user.stringID;
			}
		}
		catch( NullPointerException e )
		{
			Logger.error("ModelUserAdaptor.getUser()");
			e.printStackTrace();
		}
		catch( IllegalArgumentException e )
		{
			su.id.id = user.stringID;
		}
		
		
		
		su.accessToken = user.accessToken;
		su.authMethod = AuthenticationMethod.valueOf(user.authMethod);
		su.lastAccess = user.lastAccess;
		su.avatarUrl = user.avatarUrl;
		su.isEmailVerified = user.isEmailVerified;
		su.displayName = user.displayName;
		su.email = user.email;
		su.password = user.password;
		su.secret = user.secret;
		su.token = user.token;
		
		return su;
	}

	@Override
	public void printUser() {
		if (user != null)
		{
			Logger.info("ModelUserAdaptor:" + 
					"\n\tuser.id:\t" + user.stringID +
					"\n\tuser.provider:\t" + user.provider +
					"\n\tuser.authMethod:\t" + user.authMethod + 
					"\n\tuser.lastAccess:\t"+ user.lastAccess +
					"\n\tuser.avatarUrl:\t" + user.avatarUrl + 
					"\n\tuser.isEmailVerified\t" + user.isEmailVerified + 
					"\n\tuser.displayName:\t" + user.displayName +
					"\n\tuser.email:\t" + user.email +
					"\n\tuser.password:\t" + user.password +
					"\n\tuser.secret:\t" + user.secret  +
					"\n\tuser.token:\t" + user.token	);
		}
		else
		{
			Logger.info("ModelUserAdaptor: user is NULL");
		}
		
		
	}
}
